package com.konne.nightmare.FastPublicOpinion.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.coorchice.library.SuperTextView;
import com.konne.nightmare.FastPublicOpinion.R;

/**
 * Create by ws.
 * on Data:2022/2/11  Time:上午 10:01.
 * description: ** .
 */
public class DatabaseImportView extends FrameLayout {
    private DatabaseImportListener databaseImportListener;

    public DatabaseImportView(@NonNull Context context) {
        this(context, null);
    }

    public DatabaseImportView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DatabaseImportView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.view_database_import, null);
        addView(view);
    }

    public void setDatabaseImportListener(DatabaseImportListener databaseImportListener) {
        this.databaseImportListener = databaseImportListener;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ViewGroup vp = (ViewGroup) getChildAt(0);
        for (int i = 0; i < vp.getChildCount(); i++) {
            View view = vp.getChildAt(i);
            view.setTag(i);
            view.setOnClickListener(onClickListener);
        }
    }

    private View.OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View view) {
            int tag = (int) view.getTag();
            view.setSelected(!view.isSelected());
            if (tag == 2) {
                setType3Sel((ViewGroup) view);
            }
            if (databaseImportListener != null) {
                databaseImportListener.databaseImport(tag,view.isSelected());
            }
        }
    };

    private void setType3Sel(ViewGroup vp) {

        ViewGroup vg = (ViewGroup) vp.getChildAt(0);
        SuperTextView bg = (SuperTextView) vg.getChildAt(0);
        bg.setSolid(vp.isSelected()?0xff3396FE:0xffE9F4FF);
        ImageView pic = (ImageView) vg.getChildAt(1);
        pic.setImageResource(vp.isSelected()?R.drawable.ic_import_link_sel:R.drawable.ic_import_link);
    }

    public interface DatabaseImportListener {
        void databaseImport(int type,boolean isSel);
    }
}
